package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Basicdata;
import com.neudu.yiyang.service.BasicdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BasticData {
    @Autowired
    private BasicdataService basicdataService;
    @Autowired
    private JSONReturn jsonReturn;
    @RequestMapping("get_all_datas")
    public String getAll(@RequestBody PageInfo pageInfo) {
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            List<Basicdata> list = basicdataService.list();
            PageInfo<Basicdata> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_datas_by")
    public String getByCondition(Basicdata basicdata, PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(),pageInfo.getPageSize());

            LambdaQueryWrapper<Basicdata> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(basicdata.getId()), Basicdata::getId, basicdata.getId())
                    .eq(!ObjectUtils.isEmpty(basicdata.getCategory()), Basicdata::getCategory, basicdata.getCategory())
                    .eq(!ObjectUtils.isEmpty(basicdata.getDataCode()), Basicdata::getDataCode, basicdata.getDataCode())
                    .eq(!ObjectUtils.isEmpty(basicdata.getDataName()), Basicdata::getDataName, basicdata.getDataName());
            List<Basicdata> basicdatas = basicdataService.list(queryWrapper);
            PageInfo<Basicdata> pageInfo1 = new PageInfo<>(basicdatas);
            return jsonReturn.returnSuccess(pageInfo1);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_data")
    public String addOne(@RequestBody Basicdata basicdata){
        try {
            basicdataService.save(basicdata);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getdataby_id")
    public String getById(@RequestParam("dataid") Integer dataid){
        try {
            Basicdata basicdata = basicdataService.getById(dataid);
            return jsonReturn.returnSuccess(basicdata);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifydataby_id")
    public String modifyById(@RequestBody Basicdata basicdata){
        try {
            basicdataService.updateById(basicdata);
            return jsonReturn.returnSuccess(basicdata);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removedata_by_id")
    public String removeByid(@RequestParam("dataid") Integer dataid) {
        try {
            basicdataService.removeById(dataid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

}
